dfs and similar graphs *1700

Please click on ads to support us..

Python Code:

import threading
import sys

def main():
    n, m = map(int, input().split())

    graph = {}
    visited = [False for _ in range(n)]
    edges = []
    for _ in range(m):
        n1, n2 = map(int, input().split())
        graph[n1] = graph.get(n1, [])
        graph[n2] = graph.get(n2, [])
        graph[n1].append(n2)
        graph[n2].append(n1)
        edges.append((n1, n2))
    color = {1: 1}

    def coloring(node):
        visited[node - 1] = True
        for neighbor in graph[node]:
            if not visited[neighbor - 1]:
                color[neighbor] = 1 if color[node] == 0 else 0
                if not coloring(neighbor):
                    return False
            else:
                if color[neighbor] == color[node]:
                    return False
        return True

    if not coloring(1):
        print("NO")
    else:
        print("YES")
        binary = ""
        for n1, n2 in edges:
            if color[n1] == 1:
                binary += "0"
            else:
                binary += "1"
        print(binary)

if __name__ == '__main__':
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int m, n;
int s[N], e[N];
int flag[N];
vector<int> vc[N];
bool dfs(int i, int f) {
	if (flag[i] == -1) flag[i] = f;
	if (flag[i] != f) return false; //成环,输出no 
	for (int j = 0; j < vc[i].size(); j++) { //深度搜索与i点相连的点 
		if (flag[vc[i][j]] == -1) {
			if (!dfs(vc[i][j], f ^ 1)) return false; //使f与1异或,保证每个点只作为一次起点 
		}
		else if (flag[vc[i][j]] == f) //成环,输出no 
			return false;
	}

	return true;
}
bool check()
{
	for (int i = 0; i < n; i++) {
		if (flag[i] == 0) {
			if (!dfs(i, 1))return 0;
		}
	}
	return 1;
}
int main() 
{
	cin >> m >> n;
	memset(flag, -1, sizeof(flag));
	for (int i = 0; i < n; i++) {
		cin >> s[i] >> e[i];
		vc[s[i]].push_back(e[i]);
		vc[e[i]].push_back(s[i]);
	}
	//check();
	//for (int i = 0; i < n; i++) {
	//	cout << color[i];
	//}
	//cout << endl;
	if (dfs(1,0)) {
		cout << "Yes" << endl;
		for (int i = 0; i < n; i++) {
			if (flag[s[i]] == 1)
				cout << flag[s[i]];
			else cout << 0;
		}	
		cout << endl;
	}
	else {
		cout << "NO" << endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins